package client;

import hibernate.HibernateSessionFactory;
import hibernate.Student;
import hibernate.StudentDAO;

import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;

import org.hibernate.Session;
import org.hibernate.Transaction;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class HibernateClient extends JFrame{
	public HibernateClient() {
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel inputPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) inputPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel.add(inputPanel, BorderLayout.NORTH);
		
		indexLbl = new JLabel("input:");
		inputPanel.add(indexLbl);
		
		textField = new JTextField();
		inputPanel.add(textField);
		textField.setColumns(10);
		
		btnOk = new JButton("ok");
		
		inputPanel.add(btnOk);
		
		JPanel outputPanel = new JPanel();
		panel.add(outputPanel);
		outputPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		lblResult = new JLabel("result");
		outputPanel.add(lblResult);
		
		
		
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 3607147792519870476L;
	final private JTextField textField;
	final private JLabel indexLbl;
	final private JButton btnOk;
	String name, studentId;
	String id;
	private JLabel lblResult;
	//final private StringBuffer buf;
	public static void main(String[] args) {
		
		HibernateClient view = new HibernateClient();
		view.setBounds(100, 100, 400, 600);
		view.setVisible(true);

			view.newStudent();
			view.listStudents();
		
		
		
		
		/*
		listStudents();
		
		//new 
		String name = null,studentId = null,id = null;
		
		System.out.println("Now new a student.Please input student_id");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			studentId = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		System.out.println("Please input student's name");
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			name = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		newStudent(name, studentId);
		//list
		System.out.println("List students:");
		listStudents();
		
		//update
		
		System.out.println("Now update a student.Please input student's id");
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			id = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		System.out.println("Please input student's name");
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			name = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		System.out.println("Please input student's student_id");
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			studentId = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		updateStudent(Integer.parseInt(id), name, studentId);
		//list
		System.out.println("List students:");
		listStudents();
				
		//delete
		System.out.println("Now delete a student.Please input student's id");
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			id = br.readLine();
		} catch (IOException e) {
			System.out.println("Program error");
		}
		delStudentByStudentId(Integer.parseInt(id));
		//list
		System.out.println("List students:");
		listStudents();
		*/
	}
	
	public void newStudent() {
		
		
		indexLbl.setText("New a student.Please input name:");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				name = textField.getText();
				btnOk.removeActionListener(this);
				indexLbl.setText("Please input student_id:");
				btnOk.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						studentId = textField.getText();
						btnOk.removeActionListener(this);
						
						Session session = HibernateSessionFactory.getSession();
						Transaction transaction = session.beginTransaction();
						Student student = new Student();
						student.setName(name);
						student.setStudentId(studentId);
						session.save(student);
						transaction.commit();
						session.close();
						
						HibernateClient.this.updateStudent();
						HibernateClient.this.listStudents();
					}
				});
			}
		});
		
		
		
	}
	public void listStudents() {
		Session session = HibernateSessionFactory.getSession();
		StudentDAO studentDAO = new StudentDAO();
		List<?> studentSet = studentDAO.findAll();
		Iterator<?> studentIterator = studentSet.iterator();
		lblResult.setText("<html>");
		while (studentIterator.hasNext()) {
			Student student = (Student) studentIterator.next();
			String result="Id:"+student.getId()+"  Name:"+student.getName()+"  Student id:"+student.getStudentId()+"<br/>";
			lblResult.setText(lblResult.getText()+result);
			//System.out.println("Id:"+student.getId()+"  Name:"+student.getName()+"  Student id:"+student.getStudentId());
		}
		lblResult.setText(lblResult.getText()+"</html>");
		session.close();
	}

	public void delStudentByStudentId() {
		indexLbl.setText("Delete a student.Please input id:");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				id =textField.getText();
				btnOk.removeActionListener(this);
				try {
					Session session = HibernateSessionFactory.getSession();
					Transaction transaction = session.beginTransaction();
					StudentDAO studentDAO = new StudentDAO();
					session.delete(studentDAO.findById(new Integer(Integer.parseInt(id))));
					transaction.commit();
					session.close();
					HibernateClient.this.listStudents();
				} catch (Exception e2) {
					indexLbl.setText("Input error,please restart.");
				}
				

			}
		});
		
	}
	public void updateStudent()
	{
		indexLbl.setText("Update a student.Please input id:");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				id =textField.getText();
				btnOk.removeActionListener(this);
				indexLbl.setText("Please input name:");
				btnOk.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						name = textField.getText();
						btnOk.removeActionListener(this);
						indexLbl.setText("Please input student_id:");
						btnOk.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								studentId = textField.getText();
								btnOk.removeActionListener(this);
								try {
									Session session = HibernateSessionFactory.getSession();
									Transaction transaction = session.beginTransaction();
									StudentDAO studentDAO = new StudentDAO();
									Student student = studentDAO.findById(new Integer(Integer.parseInt(id)));
									student.setName(name);
									student.setStudentId(studentId);
									studentDAO.attachDirty(student);
									transaction.commit();
									session.close();
									
								} catch (Exception e2) {
									indexLbl.setText("Input error,please restart.");
								}
								
								HibernateClient.this.delStudentByStudentId();
								HibernateClient.this.listStudents();
							}
						});
					}
				});
			}
		});
		
		
	}
}
